#include "dynamicwidget.h"
#include "ui_dynamicwidget.h"
#include <QLineEdit>
#include <QCheckBox>
#include <QProgressBar>
#include <QProgressDialog>

DynamicWidget::DynamicWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DynamicWidget)
{
    ui->setupUi(this);
     setWindowTitle("QQ登录");

}

DynamicWidget::~DynamicWidget()
{
    delete ui;
}

void DynamicWidget::initDynamic()
{
    bool isRemember = ui->checkBoxremember->isChecked();
    if (isRemember) {
        ui->lineEditname->setText(currentName);
        ui->lineEditpwd->setText(currentPwd);
    }
    bool isAuto = ui->checkBoxauto->isChecked();
    if (isAuto && isRemember) {
        startLogin();
    }
}
void DynamicWidget::on_checkBoxremember_stateChanged(int arg1)
{
    ALOG<<"checkbox state:"<<arg1;
    switch (arg1) {
    case Qt::Checked:
        rememberUser();
        break;
    case Qt::Unchecked:
         break;
    case Qt::PartiallyChecked:
        break;
    default:
        break;
    }
}
void DynamicWidget::rememberUser()
{
    QString userName = ui->lineEditname->text();
    QString pwd = ui->lineEditpwd->text();
    if (currentName.compare(userName) == 0 && currentPwd.compare(pwd) == 0)
    {
        ALOG<<"记住usename:"<<userName<<",pwd:"<<pwd;
    } else {
        ui->checkBoxremember->setChecked(false);
        ui->checkBoxremember->setCheckState(Qt::Unchecked);
        QMessageBox::information(this,
            tr("错误消息提示"),
            tr("你输入的用户名或者密码不,不能执行记住操作！"),
            QMessageBox::Ok);
    }
}

void DynamicWidget::on_checkBoxauto_stateChanged(int arg1)
{
    ALOG<<"状态："<<arg1;
    switch (arg1) {
    case Qt::Checked:
    {
        bool isRemember = ui->checkBoxremember->isChecked();
        if (isRemember) {
            ui->lineEditname->setText(currentName);
            ui->lineEditpwd->setText(currentPwd);
            ALOG<<"登录的usename:"<<currentName<<",pwd:"<<currentPwd;
        } else {
            ui->checkBoxauto->setChecked(false);
            ui->checkBoxauto->setCheckState(Qt::Unchecked);
            QMessageBox::information(this,
                tr("错误消息提示"),
                tr("没有记住的密码！"),
                QMessageBox::Ok);
        }
    }
        break;
    case Qt::Unchecked:
         break;
    case Qt::PartiallyChecked:
        break;
    default:
        break;
    }
}

void DynamicWidget::on_pushButtonlogon_clicked()
{
    ALOG<<"开始登录";
    startLogin();
}

void DynamicWidget::startLogin()
{
    QString getName = ui->lineEditname->text();
    QString getPwd = ui->lineEditpwd->text();
    ALOG<<"当前登录的用户名："<<getName<<",当前密码："<<getPwd;
    if(currentName.compare(getName) == 0 && currentPwd.compare(getPwd) == 0) {
        ALOG<<"开始登录";
        startWechat();
    } else {
        QMessageBox::information(this,
            tr("错误消息提示"),
            tr("你输入的用户名或者密码不对！"),
            QMessageBox::Ok);
    }
}

void DynamicWidget::on_pushButtoncancel_clicked()
{
    ui->lineEditname->setText("");
    ui->lineEditpwd->setText("");
}

void DynamicWidget::startWechat()
{
    QTimer *timer = new QTimer(this);
//    showProgressBar();
    QProgressDialog *progressDlg=new QProgressDialog( QStringLiteral("正在登录......"), QStringLiteral("取消"),0,100,this);
    progressDlg->setWindowModality(Qt::WindowModal);
    progressDlg->setMinimumDuration(5);
    progressDlg->setMinimumWidth(300);
      //设置标题，可以不设置默认继承父窗口标题
    progressDlg->setWindowTitle(QString("qq"));
    progressDlg->setCancelButtonText(nullptr); //去掉取消按钮
    progressDlg->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); // 设置窗口无边框且窗口显示在最顶层
    progressDlg->show();
    timer->start(100);
    static int count = 0;
    connect(timer,&QTimer::timeout,[=](){
            count++;
            progressDlg->setValue(count);
            if(count > progressDlg->maximum())
               {
                   ALOG<<"登录成功";
                   this->hide();
                   w.show();
                   client.show();
                   timer->stop();
                   count = 0;
                   delete progressDlg;
               }

        });
}
